package iedcyt.acute.dataAccess.dao.acute1;

import iedcyt.acute.dataAccess.domain.acute1.Center_Corp;
import iedcyt.acute.dataAccess.generic.SpringDao;
import iedcyt.acute.dataAccess.idao.acute1.ICenter_CorpDao;

import java.util.ArrayList;
import java.util.List;

public class Center_CorpDao extends SpringDao implements ICenter_CorpDao  {

	@Override
	public void create() {
		//comprobamos si la tabla existe y si no la creamos
		try {
			_jdbcTemplate.queryForInt("select count(*) from Center_Corp");
		} catch (Exception e) {
			_jdbcTemplate.execute("CREATE TABLE Center_Corp (idCe numeric , idCorp numeric)");
			_jdbcTemplate.execute("CREATE INDEX idxCenter_Corp on Center_Corp (idCe, idCorp) WITH PRIMARY");
			_jdbcTemplate.execute("ALTER TABLE Center_Corp ADD FOREIGN KEY (idCe) REFERENCES Center(idCe)");
			_jdbcTemplate.execute("ALTER TABLE Center_Corp ADD FOREIGN KEY (idCorp) REFERENCES Corp(idCorp)");
		}
	}
	
	@Override
	public List<Center_Corp> selectAll() {
		
		return _jdbcTemplate.query("SELECT * FROM Center_Corp",
				new Center_CorpRowMapper());
		
	}
	
	@Override
	public void deleteAll() {
		_jdbcTemplate.update("DELETE from Center_Corp");
	}

	@Override
	public void insert(Center_Corp doc) {
	
		_jdbcTemplate.update("INSERT INTO Center_Corp (idCe, idCorp) VALUES (?,?)",
				new Object[] { doc.get_idCe(), doc.get_idCorp() });
		
	}
	
	@Override
	public List<Center_Corp> selectByIdCe(int[] ids) {
		
		if (ids.length != 0)
		{
		String where = "idCe = ? ";
		int cnt = 1;
		for (int i = 0; i < ids.length -1 ; i++)
		{
			cnt++;
			where += "OR idCe = ? ";
		}
		
		Object[] objs = new Object[cnt];
		
		for (int i = 0; i < ids.length ; i++) {
			objs[i] = ids[i];
		}
		
		return _jdbcTemplate
		.query( "SELECT * FROM Center_Corp WHERE " + where,
				objs,
				new Center_CorpRowMapper());
		}
		else return new ArrayList<Center_Corp>();
	}

		
	@Override
	public List<Center_Corp> selectByIdCorp(int[] ids) {
		if (ids.length != 0)
		{
		String where = "idCorp = ? ";
		int cnt = 1;
		for (int i = 0; i < ids.length -1 ; i++)
		{
			cnt++;
			where += "OR idCorp = ? ";
		}
		
		Object[] objs = new Object[cnt];
		
		for (int i = 0; i < ids.length ; i++)
		{
			objs[i] = ids[i];
		}
		
		return _jdbcTemplate.query("SELECT * FROM Center_Corp WHERE " + where,
				objs ,
				new Center_CorpRowMapper()); 
		}
		else return new ArrayList<Center_Corp>();
	}

	@Override
	public void delete(int idCe, int idCorp) {
		_jdbcTemplate.update("DELETE from Center_Corp WHERE idCe=? and idCorp=?",
				new Object[] { idCe, idCorp  });
	}

	/**
	 * @see iedcyt.acute.dataAccess.idao.acute1.ICenter_CorpDao#ifNotExistsInsert(iedcyt.acute.dataAccess.domain.acute1.Center_Corp)
	 */
	@Override
	public void ifNotExistsInsert(Center_Corp cc) {
		int exist = _jdbcTemplate.query("SELECT * FROM Center_Corp WHERE idCe = ? AND idCorp = ?",
				new Object[] { cc.get_idCe(), cc.get_idCorp()},
				new Center_CorpRowMapper()).size(); 
		
		if (exist == 0) {
			_jdbcTemplate.update("INSERT INTO Center_Corp (idCe, idCorp) VALUES (?,?)",
				new Object[] { cc.get_idCe(), cc.get_idCorp() });
		}
		
	}

}

